package com.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.mapper.RoleMapper;
import com.sys.mapper.UserRoleMapper;
import com.sys.pojo.Role;
import com.sys.pojo.UserRole;
import com.sys.service.UserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public List<Role> getAllRole(Integer uId) {
        if (uId != null) {
            LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserRole::getUserId, uId);
            List<UserRole> userRoles = userRoleMapper.selectList(queryWrapper);
            List<Role> roles = new ArrayList<>();
            for (UserRole userRole : userRoles) {
                roles.add(roleMapper.selectById(userRole.getRoleId()));
            }
           return roles;
        }
        return null;
    }

    @Override
    public List getRoleIdsByUserId(Integer uId) {
        LambdaQueryWrapper<UserRole> queryWrapper = Wrappers.<UserRole>lambdaQuery().select(UserRole::getRoleId).eq(UserRole::getUserId, uId);
        return userRoleMapper.selectObjs(queryWrapper);
    }
}
